﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using log4net;

using JsonServer.Interfaces;

namespace JsonServer.Server
{
    public class JsonServerBase :
        IJsonServer
    {
        #region Logging
        protected static readonly ILog _log = LogManager.GetLogger(typeof(JsonServerBase));
        #endregion

        #region State

        protected Dictionary<string, AjaxMethod> Api = new Dictionary<string, AjaxMethod>();

        #endregion

        #region CtorDtor
        static JsonServerBase()
        {
        }

        public JsonServerBase()
        {
        }
        #endregion

        protected virtual void RegisterApi(JsonServerApi api)
        {
            throw new NotImplementedException();
        }
        protected virtual void Configure(XmlNode cfg)
        {
        }

        //protected virtual void Execute(string method, Dictionary<string, string> args, Stream inputStream, Stream outputStream)
        //{
        //    try
        //    {
        //        if (Api.ContainsKey(method))
        //        {
        //            Api[method](args, inputStream, outputStream);
        //        }
        //        else
        //        {
        //            var msg = string.Format("Method '{0}()' not implemented", method);

        //            throw new NotImplementedException(msg);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        _log.Error("Unexpected Exception in jsServiceBase::Execute()", e);
        //        throw;
        //    }
        //}

        #region IJsonServer Members

        void IJsonServer.RegisterApi(JsonServerApi api)
        {
            RegisterApi(api);
        }

        void IJsonServer.Configure(XmlNode cfg)
        {
            Configure(cfg);
        }


        //void IJsonServer.Execute(string method, Dictionary<string, string> args, Stream inputStream, Stream outputStream)
        //{
        //    Execute(method, args, inputStream, outputStream);
        //}

        #endregion
    }
}
